#include "print.h"
#include "init.h"
#include "thread.h"
#include "interrupt.h"
#include "console.h"
//#include "ioqueue.h"
#include "process.h"
#include "syscall_init.h"
#include "syscall.h"
#include "stdio.h"
#include "memory.h"
#include "dir.h"
#include "fs.h"
#include "shell.h"
#include "assert.h"
#include "keyboard.h"
#include "global.h"
#include "file.h"
#include "string.h"
#include "stdio_kernel.h"

void init();

int main() 
{
    put_str("I am kernel\n");
    init_all();

/*写入用户进程*/
/*
    //uint32_t file_size = 6508;
    //uint32_t file_size = 6536;
    //uint32_t file_size = 6100;
    //uint32_t file_size = 6112;
    // uint32_t file_size = 6220;
    // uint32_t file_size = 6428;
    uint32_t file_size = 6140;
    uint32_t sec_cnt = DIV_ROUND_UP(file_size, 512);
    struct disk *sda = &channels[0].device[0];
    void *prog_buf = sys_malloc(file_size);
    ide_read(sda, 300, prog_buf, sec_cnt);
    int32_t fd = sys_open("/prog_arg", O_CREAT | O_RDWR);
    if (fd != -1)
    {
        if (sys_write(fd, prog_buf, file_size) == -1)
        {
            printk("file write error!\n");
            while (1)
                ;
        }
    }
*/
    char buf[10] = "012345678";
    int32_t fd = sys_open("/file1", O_CREAT | O_RDWR);
    if (fd != -1) {
        if (sys_write(fd, buf, 10) == -1) {
            printk("file write error\n");
            while(1);
        }
    }
    cls_screen();
    console_put_str("[rabbit@localhost /]$ ");
    while (1);
    return 0;
}

void init() 
{
    uint32_t ret_pid = fork();
    if (ret_pid) {
        printf("i am father, my pid is %d, child pid is %d\n", getpid(), ret_pid);
        while(1);
    } else {
        printf("i am child, my pid is %d, ret pid is %d\n", getpid(), ret_pid);
        my_shell();
    }
    panic("init: should not be here\n");
}